<!-- 希望没问题，等待测试 -->
<template>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>老师管理</el-breadcrumb-item>
        <el-breadcrumb-item>添加老师</el-breadcrumb-item>
      </el-breadcrumb>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学籍编号" prop="teacherId">
      <el-input v-model="ruleForm.teacherId"></el-input>
    </el-form-item>
    <el-form-item label="老师姓名" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="老师性别" prop="sex">
      <el-radio-group v-model="ruleForm.sex">
        <el-radio label="男">男</el-radio>
        <el-radio label="女">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="老师年龄" prop="age">
      <el-input v-model.number="ruleForm.age"></el-input>
    </el-form-item>
    <!-- <el-form-item label="老师班级" prop="classId">
      <el-input v-model="ruleForm.classId"></el-input>
    </el-form-item> -->
    <el-form-item label="老师学院" prop="collegeId">
      <el-input v-model="ruleForm.collegeId"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password"></el-input>
    </el-form-item>
  
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script>
  export default {
    name: "InsertteacherInfo",
    data() {
      return {
        title: "添加老师",
        ruleForm: {teacherId:'',sex:'',name:'',age:'',collegeId:'',password:''},
        rules: {
          teacherId: [
            { required: true, message: "请填写所添加老师的学籍编号" },
            //   { length:11 , message: "长度在 3 到 5 个字符" },
          ],
          name: [{ required: true, message: "请填写老师姓名" }],
          sex: [{ required: true, message: "请选择老师性别" }],
          age: [{ required: true, message: "请填写老师年龄" }],
          collegeId:[{ required: true, message: "请填写老师学院" }],
          password:[{ required: true, message: "请填写老师的密码" },{ length:11 , message: "长度在 8 到 11 个字符" }],
    
        },
      };
    },
    created(){
      if(localStorage.getItem("type") !== 'admin'){this.$router.push(`/login`);this.$message.error('权限不够');}
  },
    methods: {
      //添加老师信息
      async insertteacherInfo() {
      // JSON化
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'token':`${localStorage.getItem("token")}`
        }
      };
      try {
        const { data: res } = await this.$http.post(`api/admin/teacher/insert`,JSON.stringify(this.ruleForm),config);
        if (res.status !== 500) {
          this.$message.success('成功添加老师信息');
          // 重新调用获取表格的代码
        } else {
          this.$message.error('添加失败');
        }
      } catch (error) {
        // Handle request error
        console.error('Request Error:', error);
        this.$message.error('添加失败');
    }
      },
      //提交表单信息
      submitForm(ruleForm) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            this.insertteacherInfo();
          }
          else{
            this.$message.error('请将信息填写完整');
          }
        });
      },
      // 重置清空表单信息
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
  };
  </script>
  
  <style lang="less" scoped>
  .el-breadcrumb {
    padding-bottom: 10px;
  }
  .el-input {
    width: 40%;
  }
  /deep/ .el-textarea__inner {
    width: 40%;
    line-height: 3;
  }
  .el-form-item {
    margin: 18px 0 !important;
  }
  </style>
  